from typing import Callable, Type

from aiogram import F
from aiogram_dialog import Window
from aiogram_dialog.widgets.kbd import Back, Cancel, ScrollingGroup, Select
from aiogram_dialog.widgets.text import Format
from aiogram_dialog.widgets.utils import GetterVariant

from src.presentation.tg_bot.dialogs.client.common_handlers import set_category_id, set_country_id
from src.presentation.tg_bot.dialogs.getters.categories import categories_getter
from src.presentation.tg_bot.dialogs.getters.countries import countries_getter
from src.presentation.tg_bot.dialogs.getters.providers import providers_getter
from src.presentation.tg_bot.dialogs.widgets import LocaleText
from src.presentation.tg_bot.states import ManageProxySubscriptionSG, SelectProxySG


def get_countries_window(
    state: Type[ManageProxySubscriptionSG] | Type[SelectProxySG],
    handler: Callable = set_country_id,
    getter: GetterVariant = countries_getter,
) -> Window:
    return Window(
        LocaleText("select-country"),
        ScrollingGroup(
            Select(
                Format("{item.name}"),
                "catselsel",
                lambda country: country.id,
                "countries",
                on_click=handler,
            ),
            width=2,
            height=4,
            id="catsel",
            hide_on_single_page=True,
            when=F["user_language"] == "ru",
        ),
        ScrollingGroup(
            Select(
                Format("{item.name_eng}"),
                "catselsel",
                lambda country: country.id,
                "countries",
                on_click=handler,
            ),
            width=2,
            height=4,
            id="catsel",
            hide_on_single_page=True,
            when=F["user_language"] == "en",
        ),
        Back(LocaleText("back-btn")),
        Cancel(LocaleText("cancel-btn")),
        state=state.select_country,
        getter=getter,
    )


def get_categories_window(
    state: Type[ManageProxySubscriptionSG] | Type[SelectProxySG],
    handler: Callable = set_category_id,
    getter: GetterVariant = categories_getter,
) -> Window:
    return Window(
        LocaleText("select-category"),
        ScrollingGroup(
            Select(
                Format("{item.name}"),
                "catselsel",
                lambda category: category.id,
                "categories",
                on_click=handler,
            ),
            width=2,
            height=4,
            id="catsel",
            hide_on_single_page=True,
            when=F["user_language"] == "ru",
        ),
        ScrollingGroup(
            Select(
                Format("{item.name_eng}"),
                "catselsel",
                lambda category: category.id,
                "categories",
                on_click=handler,
            ),
            width=2,
            height=4,
            id="catsel",
            hide_on_single_page=True,
            when=F["user_language"] == "en",
        ),
        Back(LocaleText("back-btn")),
        Cancel(LocaleText("cancel-btn")),
        state=state.select_category,
        getter=getter,
    )


def get_providers_window(
    state: Type[ManageProxySubscriptionSG] | Type[SelectProxySG],
    handler: Callable,
    getter: GetterVariant = providers_getter,
) -> Window:
    return Window(
        LocaleText("select-proxy-provider"),
        ScrollingGroup(
            Select(
                Format("{item.name}"),
                "catselsel",
                lambda provider: provider.id,
                "providers",
                on_click=handler,
            ),
            width=2,
            height=4,
            id="catsel",
            hide_on_single_page=True,
            when=F["user_language"] == "ru",
        ),
        ScrollingGroup(
            Select(
                Format("{item.name_eng}"),
                "catselsel",
                lambda provider: provider.id,
                "providers",
                on_click=handler,
            ),
            width=2,
            height=4,
            id="catsel",
            hide_on_single_page=True,
            when=F["user_language"] == "en",
        ),
        Back(LocaleText("back-btn")),
        Cancel(LocaleText("cancel-btn")),
        state=state.select_provider,
        getter=getter,
    )
